package com.jack.sort;

import com.jack.utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * 冒泡排序算法学习
 * Created by LiJiakui on 17/7/12.
 */
public class BubbleSort {

    /**
     * 冒泡排序
     *
     * @param list 待排序数据集合
     * @param desc 是否倒序排列
     * @param <V>
     * @return
     */
    public static <V>V sort(List<Integer> list,boolean desc){
        for(int j=1;j<list.size();j++){
            for(int i=0;i<list.size()-j;i++){
                int temporary = list.get(i);
                if(temporary<list.get(i+1)==desc){
                    list.set(i,list.get(i+1));
                    list.set(i+1,temporary);
                }
            }
        }
        return (V) list;
    }

    //测试
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for(int i=0;i<100;i++){
            list.add(Util.random(1,200));
        }

        BubbleSort.sort(list,false);

        for(int i : list){
            System.out.println(i);
        }
    }
}
